#region Copyright Jonathan Quiros

//	
// All rights are reserved. Reproduction or transmission in whole or in 
// part, in any form or by any means, electronic, mechanical or otherwise, 
// is prohibited without the prior written consent of the copyright owner.
//
// Filename: ViewHelper.cs
//

#endregion

using System;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using Novagoal.Models;

namespace Novagoal.Website.WebApp.Helpers
{
    public static class ViewHelper
    {
        #region CONSTANTS



        #endregion

        #region PROPERTIES

        

        #endregion

        #region PUBLIC METHODS

        // ReSharper disable Asp.NotResolved
        public static MvcHtmlString GetAvatar(this HtmlHelper htmlHelper, int personId, object htmlAttributes = null)
        {
            var urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
            var photoUrl = urlHelper.Action("GetAvatar", "Users", new { personId });
            var img = new TagBuilder("img");
            img.MergeAttribute("src", photoUrl);
            img.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return new MvcHtmlString(img.ToString());
        }
        // ReSharper restore Asp.NotResolved

        public static MvcHtmlString TeamSpan(this HtmlHelper htmlHelper, Team team, int height = 20, int width = 120)
        {
            if(team == null)
            {
                return new MvcHtmlString(string.Empty);
            }
            var urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
            var flagPath = urlHelper.Content(team.FlagPath);
            var builder = new StringBuilder();
            builder.AppendFormat("<span style='width:"+width+"px; display:inline-block; font-size:12px'>");
            builder.AppendFormat("<img height='" + height + "px' style='vertical-align:middle; padding-bottom:5px' alt='{0}' src='{1}' />", team.Name, flagPath);
            builder.AppendFormat(" {0} </span>", team.Name);
            return new MvcHtmlString(builder.ToString());
        }

        public static MvcHtmlString CreateImage(this HtmlHelper htmlHelper, string filePath, object htmlAttributes = null)
        {
            var urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
            var img = new TagBuilder("img");
            img.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            if (!string.IsNullOrEmpty(filePath))
            {
                filePath = urlHelper.Content(filePath);
                img.MergeAttribute("src", filePath);
            }
            else
            {
                img.MergeAttribute("height", "1px", true);
            }
            return new MvcHtmlString(img.ToString());
        }

        public static MvcHtmlString FavoriteTeamSelection(this HtmlHelper htmlHelper, Team selectedTeam, Party party, string function, string labelText)
        {
            var label = new TagBuilder("label");
            label.MergeAttribute("style", "width:auto");
            label.InnerHtml = string.Format("<b>{0}: </b>", labelText);

            var teamSpan = selectedTeam == null ? "Click here to select a team" : TeamSpan(htmlHelper, selectedTeam, 20, 200).ToHtmlString();

            var link = new TagBuilder("a");
            link.MergeAttribute("href", function);
            link.InnerHtml = teamSpan;

            if(party.Tournament.StartAt > DateTime.UtcNow)
            {
                label.InnerHtml += link.ToString();
            }
            else
            {
                label.InnerHtml += teamSpan;
            }

            return new MvcHtmlString(label.ToString());
        }

        #endregion

        #region PRIVATE METHODS



        #endregion
    }
}